//
//  UILabel+YH.m
//  SuoShi
//
//  Created by 林宁宁 on 2019/11/9.
//  Copyright © 2019 小红许. All rights reserved.
//

#import "UILabel+YH.h"
#import <YHConstant/YHConstant+Method.h>
#import <objc/runtime.h>

@interface UILabel (_YH)
@property (nonatomic, retain) CAShapeLayer * maskLayer;
@end

@implementation UILabel (YH)


+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        yh_swizzleMethod([self class], @selector(drawTextInRect:), @selector(yh_drawTextInRect:));
        yh_swizzleMethod([self class], @selector(layoutSubviews), @selector(yh_layoutSubviews));
        yh_swizzleMethod([self class], @selector(intrinsicContentSize), @selector(yh_intrinsicContentSize));
    });
}

- (void)setYh_contentInsets:(UIEdgeInsets)yh_contentInsets {
    objc_setAssociatedObject(self, @selector(yh_contentInsets), [NSValue valueWithUIEdgeInsets:yh_contentInsets] , OBJC_ASSOCIATION_RETAIN);
}

- (UIEdgeInsets)yh_contentInsets {
    return [objc_getAssociatedObject(self, @selector(yh_contentInsets)) UIEdgeInsetsValue];
}

-(void)setMaskLayer:(CAShapeLayer *)maskLayer{
    objc_setAssociatedObject(self, @selector(maskLayer), maskLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(CAShapeLayer *)maskLayer{
    return objc_getAssociatedObject(self, @selector(maskLayer));
}


- (void)yh_drawTextInRect:(CGRect)rect{
    if(UIEdgeInsetsEqualToEdgeInsets(self.yh_contentInsets, UIEdgeInsetsZero)){
        [self yh_drawTextInRect:rect];
        return;
    }
    
    CGRect r = UIEdgeInsetsInsetRect(rect, self.yh_contentInsets);
    [self yh_drawTextInRect:r];
}


- (CGSize)yh_intrinsicContentSize {
    if(UIEdgeInsetsEqualToEdgeInsets(self.yh_contentInsets, UIEdgeInsetsZero)){
        return [self yh_intrinsicContentSize];
    }
    CGSize superContentSize = [self yh_intrinsicContentSize];
    CGFloat width = superContentSize.width + self.yh_contentInsets.left + self.yh_contentInsets.right;
    CGFloat height = superContentSize.height + self.yh_contentInsets.top + self.yh_contentInsets.bottom;
    return CGSizeMake(width, height);
}

- (void)yh_layoutSubviews {
    [self yh_layoutSubviews];
    if(UIEdgeInsetsEqualToEdgeInsets(self.yh_contentInsets, UIEdgeInsetsZero)){
        return;
    }
    [self drawShapeMaskLayer];
}



- (void)drawShapeMaskLayer{
    
    if (self.maskLayer == nil) {
        self.maskLayer = [CAShapeLayer layer];
        self.layer.mask = self.maskLayer;
    }
    CAShapeLayer * maskLayer = self.maskLayer;
    UIBezierPath * path = [UIBezierPath bezierPath];
//    path.lineWidth = self.borderWidth;
    
    CGFloat _cornerRadius = self.layer.cornerRadius;
    CGFloat v_W = self.bounds.size.width;
    CGFloat v_H = self.bounds.size.height;
    CGPoint top_right_ArcCenter = CGPointMake(v_W - _cornerRadius, _cornerRadius);
    CGPoint top_left_ArcCenter = CGPointMake(_cornerRadius, _cornerRadius);
    CGPoint bottom_left_ArcCenter = CGPointMake(_cornerRadius, v_H - _cornerRadius);
    CGPoint bottom_right_ArcCenter = CGPointMake(v_W - _cornerRadius, v_H - _cornerRadius);
    
    [path moveToPoint:CGPointMake(0, v_H - _cornerRadius)];
    
    [path addLineToPoint:CGPointMake(0, _cornerRadius)];
    [path addArcWithCenter:top_left_ArcCenter radius:_cornerRadius startAngle:-M_PI endAngle:-M_PI_2 clockwise:YES];
    
    [path addLineToPoint:CGPointMake(v_W - _cornerRadius, 0)];
    [path addArcWithCenter:top_right_ArcCenter radius:_cornerRadius startAngle:-M_PI_2 endAngle:0 clockwise:YES];
    
    [path addLineToPoint:CGPointMake(v_W, v_H - _cornerRadius)];
    [path addArcWithCenter:bottom_right_ArcCenter radius:_cornerRadius startAngle:-M_PI*2 endAngle:-M_PI-M_PI_2 clockwise:YES];
    
    [path addLineToPoint:CGPointMake(_cornerRadius, v_H)];
    [path addArcWithCenter:bottom_left_ArcCenter radius:_cornerRadius startAngle:-M_PI-M_PI_2 endAngle:-M_PI clockwise:YES];
    [path closePath];
    
    maskLayer.path = path.CGPath;
//    self.layer.mask = maskLayer;
}
@end
